package org.example.clouddemo;

import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;

@Log4j2
@SpringBootApplication()
@EnableCaching
@RequiredArgsConstructor
@EnableFeignClients // 确保这行存在,允许该模块使用Feign
@EnableScheduling // 定时执行后台任务
@EnableDiscoveryClient // 处理REST API请求
@ComponentScan(value = {"org.example.*"}) // 组件扫描,自动注册所有组件,将包下的类加载进项目中，然后通过接口访问控制
public class RecommendApplication implements ApplicationRunner {

    private final DynamicDataSourceProperties dynamicDataSourceProperties;

    public static void main(String[] args) {

        try {
            ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(RecommendApplication.class, args);
            Environment environment = configurableApplicationContext.getBean(Environment.class);
            log.info("============> 系统启动成功！后台地址：http://10.24.3.163:{}", environment.getProperty("server.port"));
        } catch (Exception e) {
            log.error(e);
        }

    }

    @Override
    public void run(ApplicationArguments args) {
        log.info("启动完成");
        log.info("数据库连接:{}", dynamicDataSourceProperties.getDatasource());
    }
}
